<template>
  <div class="login">
    <el-card class="login-card">
      <h1 class="login-title">登录</h1>
      <el-form ref="form" :model="user" label-width="80px" :rules="rules">
        <el-form-item label="用户名" prop="username">
          <el-input placeholder="请输入用户名" v-model="user.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            placeholder="请输入密码"
            type="password"
            v-model="user.password"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script setup>
import { reactive, toRefs } from "vue";
import store from "../../store";
// 定义响应式数据
const state = reactive({
  user: {
    username: "",
    password: "",
  },
  rules: {
    username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
    password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  },
});

const { user, rules } = toRefs(state);

const login = () => {
  store.dispatch("user/login", data.user);
};
</script>

<style>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  /* background-color: pink; */
}
.login-title {
  text-align: center;
  margin-bottom: 20px;
}
.login-card {
  width: 50%;
}
</style>
